package com.lutzyt.delivery.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lutzyt.delivery.bean.Commodity;
import com.lutzyt.delivery.bean.CommodityComment;
import com.lutzyt.delivery.service.CommodityCommentService;
import com.lutzyt.delivery.service.CommodityService;
import com.lutzyt.delivery.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * controller 控制器
 *
 * @author zyt
 * @date 2021-06-01
 */
@Slf4j
@RestController
@RequestMapping("/api/commodity")
public class CommodityController extends ActionAuditController<CommodityService,CommodityService,Commodity>{
  private CommodityService service;
  private CommodityCommentService commentService;

  @GetMapping("/{id}")
  public Result<Commodity> getById(@PathVariable Integer id) {
    final Commodity bean = service.getById(id);
    return Result.success(bean);
  }

  @GetMapping("/{id}/comment")
  public Result<List<CommodityComment>> comment(@PathVariable Integer id) {
    final List<CommodityComment> comments = commentService.getByCommodityId(id);
    return Result.success(comments);
  }

  @PutMapping("/{id}")
  public Result<Void> update(@RequestBody Commodity bean) {
    log.info("更新菜单:{}", bean);
    service.update(bean);
    return Result.success();
  }

  @PostMapping("/page")
  public Result<IPage<Commodity>> page(Integer pageSize, Integer pageNum) {
    final IPage<Commodity> page = service.findByPage(pageNum, pageSize);
    return Result.success(page);
  }



  @GetMapping("/hot")
  public Result<List<Commodity>> hot(){
    return Result.success(service.hot());
  }

  @DeleteMapping("/{id}")
  public Result<Void> del(@PathVariable Integer id) {
    log.info("删除:{}", id);
    service.deleteById(id);
    return Result.success();
  }

  @DeleteMapping
  public Result<Void> del(@RequestParam("id") List<String> ids) {
    log.info("pl删除:{}", ids);
    service.removeByIds(ids);
    return Result.success();
  }

  @PostMapping
  public Result<Void> insert(@RequestBody Commodity bean) {
    log.info("添加菜单:{}", bean);
    service.insert(bean);
    return Result.success();
  }

  @Autowired
  public void setCommentService(CommodityCommentService commentService) {
    this.commentService = commentService;
  }

  @Autowired
  public void setService(CommodityService service) {
    this.service = service;
  }
}
    